<?php
namespace app\api\controller\rebate;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\rebate\RebateList;
use app\common\model\rebate\Rebate as RebateModel;
use app\api\controller\Base;

use app\api\controller\Baseuser;
class Rebate extends Baseuser
{
	//初始化
    public function initialize(){
    	parent::initialize();
	}

	// /**
	//  * 创建服务费档案 createlist 
	//  * @return [type] [description]
	//  */
	// public function createlist(){
	// 	//创建档案
	// 	$data = input('post.');
	// 	if(empty($data['CorrelationId']) || empty($data['Type'])){
 //            return moerr('缺少关联订单信息');
 //        }
	// 	$id = RebateList::user_create($data);
	// 	//填写服务费信息
	// 	if(!empty($data['info'])){
	// 		RebateModel::user_create($id,$data['info']);
	// 	}
	// 	return myok($id);
	// }

	/**
	 * 增加/修改服务费信息
	 * @param  [type] $type  [description]
	 * @param  [type] $id    [description]
	 * @param  [type] $limit [description]
	 * @param  [type] $note  [description]
	 * @param  [type] $img   [description]
	 * @return [type]        [description]
	 */
	public static function createlist_self($type,$id,$limit,$img='',$note=''){
		$data['Type'] = $type;
		$data['CorrelationId'] = $id;
		$data['Limit'] = $limit;
		$data['Note'] = $note;
		$data['Img'] =  $img;
		$sb = RebateList::where('IsDelete','<>','1')->where('Type',$type)->where('CorrelationId',$id)->find();
        if(empty($sb)){
        	// 创建新记录
			RebateList::user_create($data);
			return sok('创建成功');
        }else{
        	//修改记录
        	$data['id'] = $sb['RebateListId'];
        	RebateList::user_reinfo($data);
        	return sok('修改成功');
        }
	}

	/**
	 * 修改服务费档案 relist
	 * @return [type] [description]
	 */
	public function relist(){
		$data = input('post.');
		if(empty($data['id'])){
            return moerr('缺少参数，请重新尝试');
        }
        $list = ['Note','Img','State','IsDelete','Limit'];

        $ndata = [];//需要调整的数据
        foreach ($list as $key => $value) {
        	if(is_set($data[$value])){
        		$ndata[$value] = $data[$value];
        	}
        }
        if(empty($ndata)){
        	return myerr('没有找到需要调整的数据');
        }
        RebateList::user_reinfo($data['id'],$ndata);
		return myok('修改成功');        
	}

	/**
	 * 查询服务费档案 getlist
	 * @return [type] [description]
	 */
	public function getlistinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = RebateList::getinfo($id);
		if(!$data['code']){
			return myerr($data['msg']);
		}
		$data = $data['msg'];
		return myok($data);
	}


	/**
	 * 记录服务费信息 createinfo 
	 * @return [type] [description]
	 */
	public function add_price(){
		//创建档案
		$data = input('post.');
		$id = input('id');//档案id
		//增加记录信息
		if(!empty($data)){
			$data['RebateListId'] = $id;
			RebateModel::user_create($data);
		}
		//修改档案信息 合计服务费 每次服务费后将此值向上增加
		RebateList::add_price($id,$data['Price']);
		return myok();
	}

	/**
	 * 查询服务费记录具体信息 getreinfo
	 * @return [type] [description]
	 */
	public function getreinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = RebateModel::getinfo($id);
		return myok($data);
	}

	/**
	 * 修改服务费信息 reinfo
	 * @return [type] [description]
	 */
	public function reinfo(){
		$idata  = input('post.');
		if(empty($idata['id'])){
			return myerr('缺少参数，请重新尝试');
		}
        $list = ['Note','Img','State','IsDelete','Limit','Price'];
        $ndata = [];//需要调整的数据
        foreach ($list as $key => $value) {
        	if(isset($idata[$value])){
        		$ndata[$value] = $idata[$value];
        	}
        }
        if(empty($ndata)){
        	return myerr('没有找到需要调整的数据');
        }
        $ndata['id'] = $idata['id'];
        RebateModel::user_reinfo($ndata);
		return myok('修改成功');
	}

}
